<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8"/>
    <title>在线转义html</title>
</head>
<body>

<textarea id="v1" style="width: 80%; height: 300px"></textarea>
<button id="t" value="转换" style="width: 80%; height: 50px;">转换</button>
<textarea id="v2" style="width: 80%; height: 300px"></textarea>

<script type="text/javascript">
    var v1 = document.getElementById('v1');
    var v2 = document.getElementById('v2');
    var t = document.getElementById('t');

    function html_encode(str) {
        var s = "";
        if (str.length == 0) return "";
        s = str.replace(/</g, "&lt;");
        s = s.replace(/>/g, "&gt;");
        return s;
    }

    t.addEventListener('click', function () {
        var s1 = v1.value;
        v2.value = html_encode(s1)
    });

</script>
</body>
</html>

<style></style>
<script></script>
